package com.jeff.tianti.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.cms.dto.AreaQueryDTO;
import com.jeff.tianti.cms.entity.Area;
import com.jeff.tianti.cms.service.AreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeff.tianti.cms.dto.ArticleQueryDTO;
import com.jeff.tianti.cms.dto.ColumnInfoDTO;
import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.entity.Article;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.service.ArticleService;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;

/**
 * CMS的Controller
 *
 * @author Jeff Xu
 */
@Controller
@RequestMapping("/cms")
public class CmsController {

    @Autowired
    private ColumnInfoService columnInfoService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private AreaService areaService;

    /**
     * 获取栏目列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/column/list")
    public String columnList(HttpServletRequest request, Model model) {
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String rootColumnId = request.getParameter("rootColumnId");//url
        String columnId = request.getParameter("columnId");//url
        String columnId1 = request.getParameter("columnId1");//url
        ColumnInfoQueryDTO columnInfoQuery = new ColumnInfoQueryDTO();
        columnInfoQuery.setLevel(ColumnInfo.LEVEL_ROOT);
        List<ColumnInfo> rootCoulumnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQuery);
        for (int i = 0; i < rootCoulumnInfoList.size(); i++) {
            ColumnInfo columnInfo = rootCoulumnInfoList.get(i);
            if (StringUtils.isBlank(rootColumnId)) {
                rootColumnId = columnInfo.getId();
            }
            getChildColumn(columnInfo);
        }
        ColumnInfoQueryDTO columnInfoDTO = new ColumnInfoQueryDTO();
        columnInfoDTO.setRootColumnId(rootColumnId);
        columnInfoDTO.setIsRootColumnLike(false);
        columnInfoDTO.setCode(code);
        columnInfoDTO.setName(name);
        columnInfoDTO.setColumnId(columnId);
        columnInfoDTO.setColumnId1(columnId1);
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(rootColumnId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        columnInfoQueryDTO.setCode(code);
        columnInfoQueryDTO.setName(name);
        columnInfoQueryDTO.setColumnId(columnId);
        if (StringUtils.isNotBlank(columnId)) {
            if (StringUtils.isNotBlank(columnId1)) {
                columnInfoQueryDTO.setRootColumnId(columnId1);
            } else {
                columnInfoQueryDTO.setRootColumnId(columnId);
            }
        }
        List<ColumnInfo> list = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        model.addAttribute("list", list);
        model.addAttribute("columnInfoQueryDTO", columnInfoDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_COLUMN_LIST);
        model.addAttribute("rootCoulumnInfoList", rootCoulumnInfoList);
        return "/cms/column_default_list";
    }

    public void getChildColumn(ColumnInfo columnInfo) {
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(columnInfo.getId());
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> childColumnList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        columnInfo.setChildColumnInfoList(childColumnList);
        if (CollectionUtils.isEmpty(childColumnList)) {
            return;
        } else {
            for (ColumnInfo childColumnInfo : childColumnList) {
                getChildColumn(childColumnInfo);
            }
        }
    }

    /**
     * 校验栏目编码是否存在
     *
     * @param request
     * @return
     */
    @RequestMapping("/column/ajax/validator/code")
    @ResponseBody
    public Map<String, Object> ajaxValidatorUsername(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        String code = request.getParameter("code");
        String columnId = request.getParameter("columnId");
        List<ColumnInfo> columnInfoList = this.columnInfoService.findByCodeAndId(code, columnId);
        if (columnInfoList != null && columnInfoList.size() > 0) {
            map.put("error", "编码已经存在");
        } else {
            map.put("ok", "");
        }
        return map;
    }

    /**
     * 栏目修改
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/column/edit")
    public String edit(HttpServletRequest request, Model model) {
        ColumnInfo columnInfo = null;
        String id = request.getParameter("id");
        String columnLevel = request.getParameter("columnLevel");
        String rootColumnId = request.getParameter("rootColumnId");
        String columnId = request.getParameter("columnId");
        String columnId1 = request.getParameter("columnId1");
        List<ColumnInfo> rootCoulumnInfoList = new ArrayList<>();
        ColumnInfo columnInfo1 = this.columnInfoService.find(rootColumnId);
        if (StringUtils.isNotBlank(columnId1)) {
            rootColumnId = columnId;
        }
        if (StringUtils.isBlank(columnId)) {//为空
            columnId = rootColumnId;
//            id = rootColumnId;
        }
        if ("1".equals(columnLevel)) {
            id = "";
        }
        if ("2".equals(columnLevel)) {
            id = "";
        }
        if (StringUtils.isNotBlank(id)) {
            columnInfo = this.columnInfoService.find(id);
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(rootColumnId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        rootCoulumnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        rootCoulumnInfoList.add(columnInfo1);
        model.addAttribute("columnInfo", columnInfo);//数据回显
        model.addAttribute("columnLevel", columnLevel);
        model.addAttribute("rootColumnId", rootColumnId);
        model.addAttribute("columnId", columnId);
        model.addAttribute("columnId1", columnId1);
        model.addAttribute("rootCoulumnInfoList", rootCoulumnInfoList);//父下拉列表
        return "/cms/dialog/column_default_edit";
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/column/ajax/save")
    @ResponseBody
    public AjaxResult doColumnajaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String columnLevel = request.getParameter("columnLevel");
            String parentId = request.getParameter("parentId");
            String orderNoStr = request.getParameter("orderNo");
            Integer orderNo = null;
            if (StringUtils.isNotBlank(orderNoStr)) {
                orderNo = Integer.parseInt(orderNoStr);
            }
            ColumnInfo columnInfo = null;
            if (StringUtils.isNotBlank(id)) {
                columnInfo = this.columnInfoService.find(id);
                columnInfo.setName(name);
                if (columnLevel.equals("level0")) {
                    columnInfo.setPath(columnInfo.getId());
                } else {
                    if (StringUtils.isNotBlank(parentId)) {
                        ColumnInfo parentColumnInfo = this.columnInfoService.find(parentId);
                        columnInfo.setParent(parentColumnInfo);
                    }
                }
                columnInfo.setChannel(ColumnInfo.CHANNEL_PC);
                columnInfo.setOrderNo(orderNo);
                columnInfo.setUpdateDate(new Date());
            } else {
                columnInfo = new ColumnInfo();
                columnInfo.setCode(code);
                columnInfo.setName(name);
                if (StringUtils.isNotBlank(parentId)) {
                    ColumnInfo parentColumnInfo = this.columnInfoService.find(parentId);
                    columnInfo.setParent(parentColumnInfo);
                    columnInfo.setLevel(parentColumnInfo.getLevel() + 1);
                } else {
                    columnInfo.setLevel(ColumnInfo.LEVEL_ROOT);
                }
                columnInfo.setChannel(ColumnInfo.CHANNEL_PC);
                columnInfo.setOrderNo(orderNo);
                columnInfo.setCreateDate(new Date());
                columnInfo.setDeleteFlag(ColumnInfo.DELETE_FLAG_NORMAL);
            }

            if (StringUtils.isNotBlank(id)) {
                this.columnInfoService.update(columnInfo);
            } else {
                this.columnInfoService.save(columnInfo);
                if (columnInfo != null) {
                    if (columnInfo.getParent() != null) {
                        String path = columnInfo.getParent().getId() + "/" + columnInfo.getId();
                        columnInfo.setPath(path);
                    } else {
                        columnInfo.setPath(columnInfo.getId());
                    }
                    this.columnInfoService.update(columnInfo);
                }
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 栏目删除
     *
     * @param request
     * @return
     */
    @RequestMapping("/column/ajax/delete")
    @ResponseBody
    public AjaxResult ajaxUpdColumnDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String deleteFlag = request.getParameter("deleteFlag");
            String columnId = request.getParameter("columnId");
            if (StringUtils.isNotBlank(columnId)) {
                id = columnId;
            }
            if (StringUtils.isNotBlank(id)) {

                ColumnInfo columnInfo = this.columnInfoService.find(id);
                columnInfo.setDeleteFlag(deleteFlag);
                this.columnInfoService.update(columnInfo);

                //删除该节点对应的孩子列表
                ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
                columnInfoQueryDTO.setRootColumnId(id);
                columnInfoQueryDTO.setIsRootColumnLike(false);
                List<ColumnInfo> leafColumnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
                if (leafColumnInfoList != null && leafColumnInfoList.size() > 0) {
                    for (ColumnInfo c : leafColumnInfoList) {
                        c.setDeleteFlag(ColumnInfo.DELETE_FLAG_DELETED);
                        this.columnInfoService.update(c);
                        //删除该节点对应的孩子列表
                        ColumnInfoQueryDTO childColumnInfoQueryDTO = new ColumnInfoQueryDTO();
                        childColumnInfoQueryDTO.setRootColumnId(c.getId());
                        childColumnInfoQueryDTO.setIsRootColumnLike(false);
                        List<ColumnInfo> childLeafColumnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);

                        if (!CollectionUtils.isEmpty(childLeafColumnInfoList)) {
                            childLeafColumnInfoList.forEach(columnInfo1 -> {
                                columnInfo1.setDeleteFlag(ColumnInfo.DELETE_FLAG_DELETED);
                                this.columnInfoService.update(columnInfo1);
                            });
                        }
                    }
                }

            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 获取文章列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/article/list")
    public String articleList(HttpServletRequest request, Model model) {
        String type = request.getParameter("type");
        String rootColumnId = request.getParameter("rootColumnId");
        String columnId = request.getParameter("columnId");
        String title = request.getParameter("title");
        String publisher = request.getParameter("publisher");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String createDateSortCss = request.getParameter("createDateSortCss");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setLevel(ColumnInfo.LEVEL_ROOT);
        columnInfoQueryDTO.setCode(Constants.CODE_HYZX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> rootCoulumnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfoDTO> columnInfoDTOList = new ArrayList<ColumnInfoDTO>();


        if (rootCoulumnInfoList != null && rootCoulumnInfoList.size() > 0) {
            if (StringUtils.isBlank(rootColumnId)) {
                rootColumnId = rootCoulumnInfoList.get(0).getId();
            }
            for (ColumnInfo c : rootCoulumnInfoList) {
                ColumnInfoDTO columnInfoDTO = new ColumnInfoDTO();
                columnInfoDTO.setId(c.getId());
                columnInfoDTO.setName(c.getName());

                ColumnInfoQueryDTO childColumnInfoQueryDTO = new ColumnInfoQueryDTO();
                childColumnInfoQueryDTO.setRootColumnId(c.getId());
                childColumnInfoQueryDTO.setIsRootColumnLike(false);
                List<ColumnInfo> childColumnInfoList = this.columnInfoService.queryColumnInfoList(childColumnInfoQueryDTO);
                if (childColumnInfoList != null && childColumnInfoList.size() > 0) {
                    List<ColumnInfoDTO> rColumnInfoDTOList = new ArrayList<ColumnInfoDTO>();
                    for (ColumnInfo rc : childColumnInfoList) {
                        ColumnInfoDTO rColumnInfoDTO = new ColumnInfoDTO();
                        rColumnInfoDTO.setId(rc.getId());
                        rColumnInfoDTO.setName(rc.getName());
                        rColumnInfoDTOList.add(rColumnInfoDTO);
                    }
                    columnInfoDTO.setChildColumnInfoList(rColumnInfoDTOList);
                }
                columnInfoDTOList.add(columnInfoDTO);
            }
        }

        ArticleQueryDTO articleQueryDTO = new ArticleQueryDTO();
        articleQueryDTO.setColumnId(columnId);
        articleQueryDTO.setType(type);
        articleQueryDTO.setIsFront(false);
        articleQueryDTO.setRootColumnId(rootColumnId);
        articleQueryDTO.setTitle(title);
        articleQueryDTO.setPublisher(publisher);
        articleQueryDTO.setStartDate(startDate);
        articleQueryDTO.setEndDate(endDate);
        articleQueryDTO.setCreateDateSortCss(createDateSortCss);
        articleQueryDTO.setCurrentPage(currentPage);
        articleQueryDTO.setPageSize(pageSize);

        PageModel<Article> page = this.articleService.queryArticlePage(articleQueryDTO);
        List<Map<String, Object>> statisMapList = this.articleService.queryStatisMapList(articleQueryDTO);
        Map<String, Object> statisMap = null;
        if (statisMapList != null && statisMapList.size() > 0) {
            statisMap = statisMapList.get(0);
        }
        model.addAttribute("page", page);
        model.addAttribute("type", type);
        model.addAttribute("statisMap", statisMap);
        model.addAttribute("articleQueryDTO", articleQueryDTO);
        model.addAttribute("rootCoulumnInfoList", rootCoulumnInfoList);
        model.addAttribute("columnInfoDTOList", columnInfoDTOList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_ARTICLE_LIST);

        return "/cms/article_default_list";
    }

    /**
     * 文章修改
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/article/edit")
    public String articleEdit(HttpServletRequest request, Model model) {
        Article article = null;
        String id = request.getParameter("id");
        String rootColumnId = request.getParameter("rootColumnId");
        String columnId = request.getParameter("columnId");
        String adcode = request.getParameter("adcode");
        if (StringUtils.isNotBlank(id)) {
            article = this.articleService.find(id);
            if (article != null) {
                rootColumnId = article.getRootColumnInfo().getId();
                columnId = article.getColumnInfo().getId();
            }
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setLevel(ColumnInfo.LEVEL_ROOT);
        columnInfoQueryDTO.setLevel(ColumnInfo.LEVEL_ROOT);
        columnInfoQueryDTO.setCode(Constants.CODE_HYZX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> rootCoulumnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        AreaQueryDTO areaQueryDTO = new AreaQueryDTO();
        areaQueryDTO.setLevel(1);
        List<Area> areaList = areaService.queryAreaList(areaQueryDTO);
        model.addAttribute("rootColumnId", rootColumnId);
        model.addAttribute("columnId", columnId);
        model.addAttribute("article", article);
        model.addAttribute("rootCoulumnInfoList", rootCoulumnInfoList);
        model.addAttribute("areaList", areaList);
        model.addAttribute("adcode", adcode);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_ARTICLE_LIST);

        return "/cms/article_edit";
    }

    /**
     * 文章保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/save")
    @ResponseBody
    public AjaxResult doArticlejaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String rootColumnId = request.getParameter("rootColumnId");
            String columnInfoId = request.getParameter("leafColumnId");
            String title = request.getParameter("title");
            String publisher = request.getParameter("publisher");
            String content = request.getParameter("content");
            String summary = request.getParameter("summary");
            String coverImageUrl = request.getParameter("coverImageUrl");
            String href = request.getParameter("href");
            String orderNoStr = request.getParameter("orderNo");
            String articleTypeStr = request.getParameter("articleType");
            String guidRead = request.getParameter("guidRead");
            String adcode = request.getParameter("adcode");
            String activityDetail = request.getParameter("activityDetail");
            String organizers = request.getParameter("organizers");
            String holdingTimeStr = request.getParameter("holdingTime");
            Date holdingTime =null;
            if (StringUtils.isNotBlank(holdingTimeStr)){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
                 holdingTime = sdf.parse(holdingTimeStr);
            }
            Article article = null;
            Integer orderNo = null;
            Integer articleType = null;
            ColumnInfo rootColumnInfo = null;
            ColumnInfo columnInfo = null;
            if (StringUtils.isNotBlank(rootColumnId)) {
                rootColumnInfo = this.columnInfoService.find(rootColumnId);
            }
            if (StringUtils.isNotBlank(columnInfoId)) {
                columnInfo = this.columnInfoService.find(columnInfoId);
            }
            if (StringUtils.isNotBlank(orderNoStr)) {
                orderNo = Integer.parseInt(orderNoStr);
            }
            if (StringUtils.isNotBlank(articleTypeStr)) {
                if (articleTypeStr.equals("contentType")) {
                    articleType = 0;
                } else if (articleTypeStr.equals("hrefType")) {
                    articleType = 1;
                } else if (articleTypeStr.equals("adType")) {
                    articleType = 2;
                }
            }
            if (StringUtils.isNotBlank(id)) {
                article = this.articleService.find(id);
                article.setTitle(title);
                article.setPublisher(publisher);
                article.setContent(content);
                article.setType(articleType);
                article.setGuidRead(guidRead);
                article.setRootColumnInfo(rootColumnInfo);
                article.setColumnInfo(columnInfo);
                article.setSummary(summary);
                article.setUpdateDate(new Date());
                article.setOrderNo(orderNo);
                article.setHref(href);
                article.setAdcode(adcode);
                article.setActivityDetail(activityDetail);
                article.setOrganizers(organizers);
                article.setHoldingTime(holdingTime);
                if (StringUtils.isNotBlank(coverImageUrl)) {
                    article.setCoverImageUrl(coverImageUrl);
                }
            } else {
                article = new Article();
                article.setTitle(title);
                article.setPublisher(publisher);
                article.setContent(content);
                article.setType(articleType);
                article.setRootColumnInfo(rootColumnInfo);
                article.setColumnInfo(columnInfo);
                article.setSummary(summary);
                article.setCreateDate(new Date());
                article.setViewCount(0);
                article.setDeleteFlag(ColumnInfo.DELETE_FLAG_NORMAL);
                article.setIsAudit(true);
                article.setOrderNo(orderNo);
                article.setGuidRead(guidRead);
                article.setHref(href);
                article.setAdcode(adcode);
                article.setActivityDetail(activityDetail);
                article.setOrganizers(organizers);
                article.setHoldingTime(holdingTime);
                if (StringUtils.isNotBlank(coverImageUrl)) {
                    article.setCoverImageUrl(coverImageUrl);
                }
            }

            if (StringUtils.isNotBlank(id)) {
                this.articleService.update(article);
            } else {
                this.articleService.save(article);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 根据根节点获取叶子节点
     *
     * @param request
     * @return
     */
    @RequestMapping("/column/ajax/getLeafColumn")
    @ResponseBody
    public AjaxResult getLeafColumn(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        String rootColumnId = request.getParameter("rootColumnInfoId");
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setIsRootColumnLike(false);
        columnInfoQueryDTO.setRootColumnId(rootColumnId);
        List<ColumnInfo> columnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        ajaxResult.setData(columnInfoList);
        return ajaxResult;
    }

    /**
     * 文章删除
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/delete")
    @ResponseBody
    public AjaxResult ajaxArticleDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            String[] ids = request.getParameterValues("ids");
            String deleteFlag = request.getParameter("deleteFlag");

            if (ids != null && ids.length > 0) {
                for (String id : ids) {
                    Article article = this.articleService.find(id);
                    article.setDeleteFlag(deleteFlag);
                    this.articleService.update(article);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 文章审核
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/audit")
    @ResponseBody
    public AjaxResult ajaxArticleAudit(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            Boolean isAudit = false;
            String[] ids = request.getParameterValues("ids");
            String auditFlag = request.getParameter("auditFlag");
            if (StringUtils.isNotBlank(auditFlag) && auditFlag.equals("1")) {
                isAudit = true;
            }
            if (ids != null && ids.length > 0) {
                for (String id : ids) {
                    Article article = this.articleService.find(id);
                    article.setIsAudit(isAudit);
                    this.articleService.update(article);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 文章置顶
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/top")
    @ResponseBody
    public AjaxResult ajaxArticleTop(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            Boolean isTop = false;
            String[] ids = request.getParameterValues("ids");
            String topFlag = request.getParameter("topFlag");
            if (StringUtils.isNotBlank(topFlag) && topFlag.equals("1")) {
                isTop = true;
            }
            if (ids != null && ids.length > 0) {
                for (String id : ids) {
                    Article article = this.articleService.find(id);
                    article.setIsTop(isTop);
                    this.articleService.update(article);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 跳转到flash控件开启页面
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/article/flashView")
    public String articleFlashView(HttpServletRequest request, Model model) {

        return "/cms/article_flash_view";
    }

}
